<?php

namespace app\api\model\wanlshop;

use think\Model;
use think\Request;
use traits\model\SoftDelete;

class Goods extends Model
{
    use SoftDelete;
	
    // 表名
    protected $name = 'wanlshop_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'rmb_price',
    ];

    public function getTitleAttr($value,$data){
        $request = Request::instance();
        $lang = $request->langset();
        if($lang == 'en'){
            $value = isset($data['title_en']) ? ($data['title_en'] ? $data['title_en'] : $value) : $value;
        }
        return $value;
    }

    // getImagesAttr
    public function getImageAttr($value, $data){
        $request = Request::instance();
        if (substr($request->url(), 0, 4) == '/api') {
            return cdnurl($value, true);
        }
        return $value;
    }

    public function getContentAttr($value, $data){
        $request = Request::instance();
        if (substr($request->url(), 0, 4) == '/api') {
        //    return pregReplaceImg($value, cdnurl('',true));
        }
        return $value;
    }

    public function getImagesAttr($value, $data){
        $request = Request::instance();
        if (substr($request->url(), 0, 4) == '/api') {
            $value = $value ? explode(',', $value) : [];
            $images = [];
            foreach ($value as $image) {
                $images[] = cdnurl($image, true);
            }
            return $images;
        }
        return $value;
    }
	
	// SKU
	public function getSkuAttr($value, $data)
	{

		$list = GoodsSku::where(['goods_id'=>$data['id'], /*'stock'=>['>',0],*/ 'state'=> ['=',0]])->field('id,difference,difference_en,price,market_price,group_price,stock,state,weigh,sn,sales')->select();
//
//		foreach ($list as &$value){
//            $price = json_decode($value['group_price'],true);
//                $p = [];
//            foreach ($price as $v){
//                if($v['group_id']==3){
//                    $price1=$v['price'];
//                    break;
//                }
//            }
//            $value['price'] = $price1;
//        }
//


		$list = model('app\api\model\wanlshop\GoodsSku')->getGroupSku($list,$data['id']);
		return $list;
	}
	
	// SPU
	public function getSpuAttr($value, $data)
	{
	    $list = GoodsSpu::where('goods_id',$data['id'])->field('id,name,name_en,item,item_en')->select();
	    $list = model('app\api\model\wanlshop\GoodsSpu')->getConvertSpu($list);
		return $list;
	}

	//获取活动
	public function getActivityAttr($value,$data){
        $activity = model('app\api\model\wanlshop\Activity')->checkActivity($data['id']);
        return $activity ? $activity['type'] : false;
    }

    public function getPriceAttr($value,$data){
        $price = model('app\api\model\wanlshop\Goods')->getMemberPrice($data['id']);
        return $price ? $price : $value;
    }

    public function getRmbPriceAttr($value,$data){
        $currency_rate = model('app\api\model\wanlshop\Pay')->getCurrencyRate('',0);
        $price = model('app\api\model\wanlshop\Goods')->getMemberPrice($data['id']);
        return bcmul($price, $currency_rate, 2);
    }
	
	// 获取评论
	public function getCommentListAttr($value, $data)
	{	
		$comment = new GoodsComment;
		$where['goods_id'] = $data['id'];
		$list = $comment
			->where($where)
			->order('createtime desc')
			->field('id,user_id,content,suk,images,score')
			->limit(1) // 默认展示几条评论
			->select();
		foreach ($list as $row) {
		    $row->user->visible(['id','nickname','avatar']);
		}
		return [
			'figure' => $comment->where($where)->where('images','neq', '')->count(), //有图
			// 'tag' => array_count_values($comment->where($where)->limit(100)->column('tag')), //评论标签
			'data' => $list 
		];
	}
	
	
	// 类目属性 格式化
	public function getCategoryAttributeAttr($value)
	{	
		return json_decode($value,true);
	}
	
	// 品牌
	public function brand()
	{
	    return $this->belongsTo('app\api\model\wanlshop\Brand', 'brand_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 运费模板
	public function freight()
	{
	    return $this->belongsTo('app\api\model\wanlshop\ShopFreight', 'freight_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 运费模板
	public function freightdata()
	{
	    return $this->belongsTo('app\api\model\wanlshop\ShopFreightData', 'freight_id', 'freight_id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 店铺
	public function shop()
	{
	    return $this->belongsTo('app\api\model\wanlshop\Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 类目
	public function category()
	{
	    return $this->belongsTo('app\api\model\wanlshop\Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	// 店铺类目
	public function shopsort()
	{
	    return $this->belongsTo('app\api\model\wanlshop\ShopSort', 'shop_category_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}
	
	public function setParentInc($field){
        if($this->parent_id > 0){
            Goods::where([
                'id' => $this->parent_id
            ])->setInc($field);
        }
    }

    //获取商品列表,会员显示价格
    public function getMemberPrice($goods_id){
	    $goodsSkuModel = new GoodsSku();
	    $skuList = $goodsSkuModel->getSkuMember($goods_id);
	    //dump(json_encode($skuList,JSON_UNESCAPED_UNICODE));die;
	    if($skuList){
            $price = array_column($skuList,'price');
            $price = $price ?  min($price) : 0;
        }
        return $price ?? 0;
    }


}
